package com.erhuogo.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.bean.Response;
import com.erhuogo.entity.City;
import com.erhuogo.entity.Province;
import com.erhuogo.enumerate.Result;
import com.erhuogo.service.CityService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 城市Action
 * @author aibinxiao
 * @date 2016年10月26日 上午11:29:17
 */
public class CityAction extends ActionSupport {
	private static final long serialVersionUID = 4461518489752107855L;
	private static Logger logger = LoggerFactory.getLogger(CityAction.class);
	
	// 模型驱动使用的对象
	private City city;

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}
	
	// 注入城市Service
	private CityService cityService;

	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}
	
	// 根据省份Id获取到所有的城市
	private Integer provinceId;

	public Integer getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(Integer provinceId) {
		this.provinceId = provinceId;
	}
	
	// 返回页面数据
	private Response response = new Response();

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}

	/**
	 * 1.根据省份id获取城市列表
	 * @return
	 */
	public String getCitysByProId(){
		List<City> listCitys = cityService.findCitysByProId(provinceId);
		if(listCitys != null && listCitys.size()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("获取城市列表成功");
			response.setData(listCitys);
		} else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("获取城市列表失败/列表为空");
			response.setData(listCitys);
		}
		return "findCitysByProId";
	}
	
	/**
	 * 2.分页条件查询城市
	 * @return
	 */
	public String listPage(){
		String page = ServletActionContext.getRequest().getParameter("page");
		String rows = ServletActionContext.getRequest().getParameter("rows");
		String id = ServletActionContext.getRequest().getParameter("id");
		String name = ServletActionContext.getRequest().getParameter("name");
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");
		String provinceId = ServletActionContext.getRequest().getParameter("provinceId");
		GridPageBean<City> listPage = cityService.findByPage(page, rows, id, name, startDate, endDate, provinceId);
		if(listPage!=null && listPage.getRecords()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		response.setData(listPage);
		return "listPageSUCCESS";
	}
	
	/**
	 * 3.根据id查询城市
	 * @return
	 */
	public String findById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		City city = cityService.findById(id);
		if(city!=null){
			response.setData(city);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		return "findByIdSUCCESS";
	}
	
	/**
	 * 4.保存或者更新城市信息
	 * @return
	 */
	public String saveCity(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String name = ServletActionContext.getRequest().getParameter("name");
		String provinceId = ServletActionContext.getRequest().getParameter("provinceId");
		try {
			cityService.save(id,name,provinceId);
			response.setResult(Result.SUCCESS.toString());
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "saveSUCCESS";
	}
	
	/**
	 * 5.开启或者屏蔽城市信息
	 * @return
	 */
	public String updateStatusById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String status = ServletActionContext.getRequest().getParameter("status");
		try {
			cityService.updateStatus(id,status);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("更新状态成功");
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			response.setMessage("更新状态失败");
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "updateStatusSUCCESS";
	}
	
}
